package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.rrd.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.tongdun.TongDunRiskRecord;
import com.ucredit.babel.common.model.third.tongdun.TongDunRiskResult;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Date;

/**
 * 同盾-风险决策
 *
 * @author guoqiang
 */

public class TongDunRiskV2Parse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();
    private final int feature = SerializerFeature.WRITE_MAP_NULL_FEATURES;

    /**
     * 同盾-风险决策
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TongDunRiskRecord obj = (TongDunRiskRecord) model;
        TongDunRiskResult risk_obj = obj.getFraud_result() == null ? new TongDunRiskResult() : obj.getFraud_result();
        StringBuilder sb = getBaseInfo(obj, successful);

        StringBuilder TxSb = new StringBuilder(500);
        TxSb.append(formatValue(risk_obj.getSuccess())).append(CRT001);
        TxSb.append(formatValue(risk_obj.getReason_code())).append(CRT001);
        TxSb.append(formatValue(risk_obj.getFinal_score())).append(CRT001);
        TxSb.append(formatValue(risk_obj.getFinal_decision())).append(CRT001);
        TxSb.append(formatValue(risk_obj.getSeq_id())).append(CRT001);
        TxSb.append(formatValue(risk_obj.getRisk_type())).append(CRT001);
        TxSb.append(risk_obj.getGeoip_info() == null ? "" : JSON.toJSONString(risk_obj.getGeoip_info(), feature)).append(CRT001);
        TxSb.append(risk_obj.getDevice_info() == null ? "" : JSON.toJSONString(risk_obj.getDevice_info(), feature)).append(CRT001);
        TxSb.append(risk_obj.getAttribution() == null ? "" : JSON.toJSONString(risk_obj.getAttribution(), feature)).append(CRT001);
        TxSb.append(risk_obj.getPolicy_set() == null ? "" : JSON.toJSONString(risk_obj.getPolicy_set(), feature)).append(CRT001);
        TxSb.append(formatValue(risk_obj.getPolicy_set_name())).append(CRT001);
        TxSb.append(formatValue(risk_obj.getPolicy_name())).append(CRT001);
        TxSb.append(risk_obj.getHit_rules() == null ? "" : JSON.toJSONString(risk_obj.getHit_rules(), feature)).append(CRT001);
        TxSb.append(formatValue(risk_obj.getSpend_time())).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + TxSb + txSb).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tongdun_risk_v2_inc";
    }

    @Override
    public String getSubType() {
        return ".main";
    }

    @Override
    public String getType() {
        return RecordType.TONGDUN_RISK_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "同盾-风险决策";
    }
}
